 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Services;

import database.daos.ProductDAO;
import database.daos.ProviderDAO;
import database.daos.ProviderProductDAO;
import database.daos.DAOException;
import Services.exception.AlreadyExistBeanException;
import Services.exception.EntityNotFoundException;
import Services.exception.ServiceException;
import Services.exception.UnableToConnectException;
import Validations.ProductValidation;
import Validations.ProviderProductValidation;
import Validations.ProviderValidation;
import beans.ProductBean;
import beans.ProviderBean;
import beans.ProviderProductBean;

/**
 *
 * @author Anthar
 */
public class ProviderServices {

    private ProviderValidation provValidation;
    private ProductValidation prodValidation;
    private ProviderProductValidation provProdValidation;
    private ProviderDAO providerDAO;
    private ProductDAO productDAO;
    private ProviderProductDAO provProdDAO;
    private ProductServices productService;
    private ProviderProductServices providerProdService;

    public ProviderServices() {
        provValidation = new ProviderValidation();
        prodValidation = new ProductValidation();
        providerDAO = new ProviderDAO();
        productDAO = new ProductDAO();
        provProdDAO = new ProviderProductDAO();
        provProdValidation = new ProviderProductValidation();
        productService = new ProductServices();
        providerProdService = new ProviderProductServices();

    }

    public ProviderBean searchProvider(String RFC) throws ServiceException{
        try {
            return getProvDAO().searchProvider(RFC);
        } catch (DAOException ex) {
            throw new UnableToConnectException(ex.getExceptionMessage());
        }
        
    }

    public void addNewProduct(ProductBean product, int idProvider) throws  ServiceException, DAOException{

        final ProviderProductBean productProvider = new ProviderProductBean(product.getProductId(),idProvider);
        if(!getProdValidation().existProduct(product.getProductId())){
            try {
                getProductDAO().insert(product);                
                getProvProdDAO().insert(productProvider);
            }catch (DAOException ex) {
                throw new UnableToConnectException(ex.getMessage());
            }                
        }
        else{
            if(!getProviderProdValidation().existProviderProduct(product.getProductId(),idProvider)){
                try {
                    getProvProdDAO().insert(productProvider);
                }catch (DAOException ex) {
                  throw new UnableToConnectException(ex.getMessage());
                }
            }
            else{
                throw new AlreadyExistBeanException("Product");
            }
            
           }      
    }

     public void deleteProduct(int productId) throws ServiceException, DAOException{

        if(getProdValidation().existProduct(productId)){
            try {
                getProductDAO().deleteProduct(productId);
            } catch (DAOException ex) {
              throw new UnableToConnectException(ex.getMessage());
            }
          }
        else{
            throw new EntityNotFoundException("Producto");
        }

    }

    
    public void modifyProductInformation(ProductBean product) throws ServiceException, DAOException{

        if(getProdValidation().existProduct(product.getProductId())){
            try {
                getProductDAO().updateProduct(product);
            } catch (DAOException ex) {
                throw new UnableToConnectException(ex.getMessage());
            }
          }
        else{
           throw new EntityNotFoundException("Producto");
        }
    }


    
    
    

    /**************************
     * CONJUNTO DE GETS Y SETS
     **************************/
    /**
     * @return the validation
     */
    public ProviderValidation getProvValidation() {
        return provValidation;
    }

    /**
     * @param validation the validation to set
     */
    public void setProvValidation(ProviderValidation validation) {
        this.provValidation = validation;
    }

    /**
     * @return the provDAO
     */
    public ProviderDAO getProvDAO() {
        return providerDAO;
    }

    /**
     * @param provDAO the provDAO to set
     */
    public void setProvDAO(ProviderDAO provDAO) {
        this.providerDAO = provDAO;
    }

    /**
     * @return the prodValidation
     */
    public ProductValidation getProdValidation() {
        return prodValidation;
    }

    /**
     * @param prodValidation the prodValidation to set
     */
    public void setProdValidation(ProductValidation prodValidation) {
        this.prodValidation = prodValidation;
    }

    /**
     * @return the productDAO
     */
    public ProductDAO getProductDAO() {
        return productDAO;
    }

    /**
     * @param productDAO the productDAO to set
     */
    public void setProductDAO(ProductDAO productDAO) {
        this.productDAO = productDAO;
    }

    /**
     * @return the provProdDAO
     */
    public ProviderProductDAO getProvProdDAO() {
        return provProdDAO;
    }

    /**
     * @param provProdDAO the provProdDAO to set
     */
    public void setProvProdDAO(ProviderProductDAO provProdDAO) {
        this.provProdDAO = provProdDAO;
    }

    /**
     * @return the provProdValidation
     */
    public ProviderProductValidation getProviderProdValidation() {
        return provProdValidation;
    }

    /**
     * @param provProdValidation the provProdValidation to set
     */
    public void setProvProdValidation(ProviderProductValidation provProdValidation) {
        this.provProdValidation = provProdValidation;
    }

    /**
     * @return the productService
     */
    public //private ProviderProductBean providerProduct;
    ProductServices getProductService() {
        return productService;
    }

    /**
     * @param productService the productService to set
     */
    public void setProductService(ProductServices productService) {
        this.productService = productService;
    }

    /**
     * @return the providerProdService
     */
    public ProviderProductServices getProviderProdService() {
        return providerProdService;
    }

    /**
     * @param providerProdService the providerProdService to set
     */
    public void setProviderProdService(ProviderProductServices providerProdService) {
        this.providerProdService = providerProdService;
    }


   
}
